<script type="text/javascript">
	var CommonJs = {
		CommonLibrary(){
			if(this.ParamLibrary.data.length==0){
				this.ParamImages.loading = false;
				return false;
			}
			this.ParamLibrary.fdata = [{
					text: '全部库',
					value: '',
					download:0,
					share:0,
				}];
			for(var i in this.ParamLibrary.data){
				var item = this.ParamLibrary.data[i];
				var str = {
					text: item['appname'],
					value: item['appid'],
					download:parseInt(item['download']),
					share:parseInt(item['share']),
				};
				this.ParamLibrary.fdata.push(str);
			}
			if(this.ParamLibrary.fdata.length == 1){
				var data = this.ParamLibrary.fdata[0];
				this.ParamLibrary.activeId = data['appid'];
				this.ParamLibrary.activeTxt = data['appname'];
			}
			this.CommonhandleHash('refresh');
		},
		CommonhandleHash(isreload){
			var self = this;
			
			if(isreload=="refresh"){
				var param = (location.hash || "").replace(/^\?/,'').split("&");
				for(var i=0; i<param.length; i++){
					var item = param[i].split("=");
					if(item.length == 2){
						if(i==0){
							item[0]=item[0].replace("#","");
						}
						var name = item[0];
						var value = item[1];
						var isget = true;
						// if(self.ParamScreen.data.length == 0 && self.ParamScreen.dataTypes.indexOf(name) > -1){
						// 	continue;
						// }
						switch(name){
							case 'appid':
								self.ParamLibrary.activeId = value;
							break;
							case 'keyword':
								self.ParamSearch.keyword = decodeURI(value);
							break;
							case 'asc':
								self.ParamSort.sortorder.value = value;
							break;
							case 'order':
								self.ParamSort.Sorttype.value = parseInt(value);
							break;
							case 'classify':
								self.ParamScreen.value.classify.value = value.split(',');
								self.ParamScreen.value.classify.realvalue = value.split(',');
							break;
							case 'tag':
								var fval = value.split(',');
								for(var t in fval){
									self.ParamScreen.value.tag.value.push(parseInt(fval[t]));
								}
							break;
							case 'color':
								self.ParamScreen.value.color.value = value;
							break;
							case 'persion':
								self.ParamScreen.value.color.persion = value;
							break;
							case 'link':
								self.ParamScreen.value.link.value = value;
							break;
							case 'linkval':
								self.ParamScreen.value.link.input = value;
							break;
							case 'desc':
								self.ParamScreen.value.desc.value = value;
							break;
							case 'descval':
								self.ParamScreen.value.desc.input = value;
							break;
							case 'duration':
								var p = value.split('_');
								self.ParamScreen.value.duration.start = p[0];
								self.ParamScreen.value.duration.end = p[1];
							break;
							case 'dunit':
								self.ParamScreen.value.duration.unit = value;
							break;
							case 'wsize':
								var p = value.split('_');
								self.ParamScreen.value.size.wstart = p[0];
								self.ParamScreen.value.size.wend = p[1];
							break;
							case 'hsize':
								var p = value.split('_');
								self.ParamScreen.value.size.hstart = p[0];
								self.ParamScreen.value.size.hend = p[1];
							break;
							case 'ext':
								self.ParamScreen.value.ext.value = value.split(',');
							break;
							case 'shape':
								var arr = value.split(',');
								var farr = [];
								for(var s in arr){
									for(var ss in self.ParamScreen.value.shape.fdata){
										var sitem = self.ParamScreen.value.shape.fdata[ss];
										if(sitem.name == decodeURI(arr[s])){
											farr.push(parseInt(sitem.val));
										}
									}
								}
								self.ParamScreen.value.shape.value = farr;
							break;
							case 'shapesize':
								self.ParamScreen.value.shape.value.push('custom');
								var arr = value.split(':');
								self.ParamScreen.value.shape.sizemin = arr[0];
								self.ParamScreen.value.shape.sizemax = arr[1];
							break;
							case 'grade':
								var arr = value.split(',');
								var farr = [];
								for(var g in arr){
									if(decodeURI(arr[g]) == '未评分'){
										farr.push(0);
									}else{
										farr.push(parseInt(arr[g]));
									}
								}
								self.ParamScreen.value.grade.value = farr;
							break;
							case 'btime':
							case 'dateline':
							case 'mtime':
								var len = decodeURI(value).split('_');
								if(len.length>1){
									self.ParamScreen.value[name].value = '自定义';
									self.ParamScreen.value[name].customvalue = len[0]+' 至 '+len[1];
									var start = len[0].split('-');
									var end = len[1].split('-');
									var defaulttime = [new Date(start[0], start[1], start[2]) ,new Date(end[0], end[1], end[2])];
									self.ParamScreen.value[name].defaultdate = defaulttime;
								}else{
									self.ParamScreen.value[name].value = decodeURI(value);
								}
							break;
							
						}
					}
				}
			}
			if(isreload!="scroll"){
				if(jQuery('.pic-container-scroll').length){
					jQuery('.pic-container-scroll')[0].scrollTop = 0;
				}
				self.ParamImages.type = 'refresh';
				self.ParamBasic.page = 1;
				self.ParamBasic.total = 0;
				self.ParamBasic.totalpage = 0;
				self.ParamImages.loading = true;
			}else{
				self.ParamImages.valloading = true;
			}
			var hash = '';
			self.ParamData = {
				hassub:1
			};
			for(var x in self.ParamDataInit){
				var value = self.ParamDataInit[x];
				switch(x){
					case 'appid':
						if(self.ParamLibrary.activeId){
							self.ParamData['appid'] = self.ParamLibrary.activeId;
							hash += 'appid='+ self.ParamLibrary.activeId;
						}
					break;
					case 'keyword':
						if(value != self.ParamSearch.keyword){
							self.ParamData['keyword'] = self.ParamSearch.keyword;
							if(hash) hash += '&';
							hash += 'keyword='+ self.ParamSearch.keyword;
						}
					break;
					case 'classify':
						
						if(self.ParamScreen.value.classify.realvalue && self.ParamScreen.value.classify.realvalue.length){
							
							self.ParamData['fids'] = self.ParamScreen.value.classify.realvalue.join(',');
							if(hash) hash += '&';
							hash += 'classify='+ self.ParamScreen.value.classify.realvalue.join(',');
						}
					break;
					case 'tag':
						if(self.ParamScreen.value.tag.value && self.ParamScreen.value.tag.value.length){
							self.ParamData['tag'] = self.ParamScreen.value.tag.value.join(',');
							if(hash) hash += '&';
							hash += 'tag='+ self.ParamScreen.value.tag.value.join(',');
						}
					break;
					case 'color':
						if(self.ParamScreen.value.color.value){
							self.ParamData['color'] = self.ParamScreen.value.color.value;
							self.ParamData['persion'] = self.ParamScreen.value.color.persion;
							if(hash) hash += '&';
							hash += 'color='+ self.ParamScreen.value.color.value;
							if(hash) hash += '&';
							hash += 'persion='+ self.ParamScreen.value.color.persion;
						}
					break;
					case 'link':
						if(value != self.ParamScreen.value.link.value){
							self.ParamData['link'] = self.ParamScreen.value.link.value;
							if(hash) hash += '&';
							hash += 'link='+ self.ParamScreen.value.link.value;
							if(parseInt(self.ParamScreen.value.link.value)>0 && self.ParamScreen.value.link.input){
								if(hash) hash += '&';
								hash += 'linkval='+ self.ParamScreen.value.link.input;
							}
						}
					break;
					case 'desc':
						if(value != self.ParamScreen.value.desc.value){
							self.ParamData['desc'] = self.ParamScreen.value.desc.value;
							if(hash) hash += '&';
							hash += 'desc='+ self.ParamScreen.value.desc.value;
							if(parseInt(self.ParamScreen.value.desc.value)>0 && self.ParamScreen.value.desc.input){
								if(hash) hash += '&';
								hash += 'descval='+ self.ParamScreen.value.desc.input;
							}
						}
					break;
					case 'duration':
						var start = self.ParamScreen.value.duration.start;
						var end = self.ParamScreen.value.duration.end;
						var fval = '';
						if(start || end){
							fval = start+'_'+end;
						}
						if(value != fval){
							self.ParamData['duration'] = fval;
							if(hash) hash += '&';
							hash += 'duration='+ fval;
							if(hash) hash += '&';
							hash += 'dunit='+ self.ParamScreen.value.duration.unit;
						}
					break;
					case 'wsize':
						var start = self.ParamScreen.value.size.wstart;
						var end = self.ParamScreen.value.size.wend;
						var fval = '';
						if(start || end){
							fval = start+'_'+end;
						}
						if(value != fval){
							self.ParamData['wsize'] = fval;
							if(hash) hash += '&';
							hash += 'wsize='+ fval;
						}
					break;
					case 'hsize':
						var start = self.ParamScreen.value.size.hstart;
						var end = self.ParamScreen.value.size.hend;
						var fval = '';
						if(start || end){
							fval = start+'_'+end;
						}
						if(value != fval){
							self.ParamData['hsize'] = fval;
							if(hash) hash += '&';
							hash += 'hsize='+ fval;
						}
					break;
					case 'ext':
						var fval = self.ParamScreen.value.ext.value.join(',');
						if(fval){
							self.ParamData['ext'] = fval;
							if(hash) hash += '&';
							hash += 'ext='+ fval;
						}
					break;
					case 'shape':
						var index = self.ParamScreen.value.shape.value.indexOf('custom');
						var newval = JSON.parse(JSON.stringify(self.ParamScreen.value.shape.value));
						if(index > -1){
							newval.splice(index,1);
						}
						var fval = newval.join(',');
						if(fval){
							self.ParamData['shape'] = fval;
							if(hash) hash += '&';
							var farr = [];
							for(var s in self.ParamScreen.value.shape.value){
								var sitem = self.ParamScreen.value.shape.value[s];
								if(sitem == 'custom'){
									continue;
								}
								for(var ss in self.ParamScreen.value.shape.fdata){
									var ssitem = self.ParamScreen.value.shape.fdata[ss];
									if(parseInt(ssitem.val) == parseInt(sitem)){
										farr.push(ssitem.name);
									}
								}
							}
							hash += 'shape='+ farr.join(',');
						}
					break;
					case 'shapesize':
						var index = self.ParamScreen.value.shape.value.indexOf('custom');
						var sizemin = self.ParamScreen.value.shape.sizemin;
						var sizemax = self.ParamScreen.value.shape.sizemax;
						if(index>-1 && sizemin && sizemax){
							self.ParamData['shapesize'] = sizemin+':'+sizemax;
							if(hash) hash += '&';
							hash += 'shapesize='+ sizemin+':'+sizemax;
						}
						
					break;
					case 'grade':
						var fval = self.ParamScreen.value.grade.value.join(',');
						var index = self.ParamScreen.value.grade.value.indexOf(0);
						var newval = JSON.parse(JSON.stringify(self.ParamScreen.value.grade.value));
						
						if(index > -1){
							newval.splice(index,1,'未评分');
						}
						if(fval){
							self.ParamData['grade'] = fval;
							if(hash) hash += '&';
							hash += 'grade='+ newval.join(',');
						}
						
					break;
					case 'btime':
					case 'dateline':
					case 'mtime':
						var fval = self.ParamScreen.value[x];
						if(fval.value){
							if(fval.value == '自定义'){
								if(fval.customvalue){
									var time = fval.customvalue.split('至');
									var ftime = $.trim(time[0])+'_'+$.trim(time[1]);
									self.ParamData[x] = ftime;
									if(hash) hash += '&';
									hash += x+'='+ ftime;
								}
							}else{
								if(hash) hash += '&';
								hash += x+'='+ fval.value;
								self.ParamData[x] = GetDateVal(fval.value);
							}
						}
					break;
					case 'order':
						self.ParamData['order'] = self.ParamSort.Sorttype.value;
						if(value != self.ParamSort.Sorttype.value){
							if(hash) hash += '&';
							hash += 'order='+ self.ParamSort.Sorttype.value;
						}
					break;
					case 'asc':
						self.ParamData['asc'] = self.ParamSort.sortorder.value;
						if(value != self.ParamSort.sortorder.value){
							if(hash) hash += '&';
							hash += 'asc='+ self.ParamSort.sortorder.value;
						}
					break;
					case 'page':
						if(isreload=='scroll'){
							self.ParamBasic.page += 1;
						}
						self.ParamData[x] = self.ParamBasic[x];
						// if(value != self.ParamBasic[x]){
						// 	if(hash) hash += '&';
						// 	hash += x+'='+ self.ParamBasic[x];
						// }
					break;
				}
			}
			self.ParamData['perpage'] = self.ParamBasic['perpage'];
			if(isreload != 'refresh'){
				window.location.hash = hash;
			}
			this.CommonGetData(isreload);
			
		},
		CommonSetBadge(){
			var self = this;
			for(var i in self.ParamScreen.data){
				var item = self.ParamScreen.data[i];
				var type = item.type;
				switch(type){
					case 'classify':
						var fitem = self.ParamScreen.value[type];
						if(fitem.value.length){
							item.dot = true;
						}else{
							item.dot = false;
						}
					break;
					case 'ext':
					case 'shape':
					case 'grade':
						var fitem = self.ParamScreen.value[type];
						if(fitem.value.length){
							item.badge = fitem.value.length;
						}else{
							item.badge = '';
						}
					break;
					case 'tag':
						if(item.grouptag){
							item.badge = item.value.length || '';
						}else{
							item.badge = self.ParamScreen.value[type].value.length || '';
						}
					break;
					case 'color':
					case 'link':
					case 'desc':
						item.dot = self.ParamScreen.value[type].value?true:false;
					break;
					case 'duration':
						if(self.ParamScreen.value[type].start || self.ParamScreen.value[type].end){
							item.dot = true;
						}else{
							item.dot = false;
						}
					break;
					case 'size':
						var fitem = self.ParamScreen.value[type];
						if(fitem.wstart || fitem.wend || fitem.hstart || fitem.hend){
							item.dot = true;
						}else{
							item.dot = false;
						}
					break;
					case 'btime':
					case 'dateline':
					case 'mtime':
						if(self.ParamScreen.value[type].value || self.ParamScreen.value[type].customvalue){
							item.dot = true;
						}else{
							item.dot = false;
						}
					break;
				}
			}
			self.CommonSetFooterBadge();
		},
		CommonSetFooterBadge(){
			var self = this;
			var num = 0;
			for(var i in self.ParamScreen.data){
				var item = self.ParamScreen.data[i];
				switch(item.type){
					case 'tag':
					case 'ext':
					case 'shape':
					case 'grade':
						if(item.badge && parseInt(item.badge)){
							num++;
						}
					break;
					case 'classify':
					case 'color':
					case 'link':
					case 'desc':
					case 'duration':
					case 'size':
					case 'btime':
					case 'dateline':
					case 'mtime':
						if(item.dot){
							num++;
						}
					break;
				}
			}
			self.ParamScreen.ActiveNum = num || '';
			if(self.ParamSearch.keyword || self.ParamScreen.ActiveNum){
				self.ParamFooter.data.clear.show = true;
			}else{
				self.ParamFooter.data.clear.show = false;
			}
		},

		CommonGetData(isreload){
			var self = this;
			if(self.ParamSearch.keyword || self.ParamScreen.ActiveNum){
				self.ParamFooter.data.clear.show = true;
			}else{
				self.ParamFooter.data.clear.show = false;
			}
			$.post(MOD_URL+'&op=filelist&operation=filelist',self.ParamData,function(data){
				var information = data.data;
				var ftotal = Math.ceil(information.param.total / information.param.perpage);
				if(isreload=='scroll'){
					self.ParamImages.type = 'append';
					self.ParamImages.data.push.apply(self.ParamImages.data,information.data);
					self.ParamBasic.totalpage = ftotal;
				}else{
					self.ParamImages.type = 'refresh';
					self.ParamImages.data = information.data;
					self.ParamBasic.totalpage = ftotal<1?1:ftotal;
				}
				self.ParamBasic.total = information.param.total;
				if(isreload == 'refresh'){
					if(information.param.tagdata){
						var tstr = {};
						for(var t in information.param.tagdata){
							var titem = information.param.tagdata[t];
							if(!tstr[titem.cid]){
								tstr[titem.cid] = [];
							}
							tstr[titem.cid].push(titem.tid);
						}
						self.ParamScreen.value.tag.refreshval = tstr;
					}
					self.CommonGetScreenData();
				}
			},'json');
		},
		CommonAddkeyword(keyword,ktype){//添加关键词次数
			var self = this;
			jQuery.post(MOD_URL+'&op=ajax&operation=addsearch',{
				appid:self.ParamLibrary.activeId,
				keyword:keyword,
				ktype:ktype
			});
		},
		CommonGetScreenData(){
			var self = this;
			var param = {
				appid:self.ParamLibrary.activeId
			};
			if(self.ParamScreen.value.classify.realvalue && self.ParamScreen.value.classify.realvalue.length){
				param['fids'] = self.ParamScreen.value.classify.realvalue.join(',');
			}
			$.post(MOD_URL+'&op=ajax&operation=getscreen',param,function(data){
				if(data.success){
					var fdata = data.data;
					var arr = [];
					var grouparr = {};
					var index = 0;
					
					for(var i in fdata){
						var item = fdata[i];
						if(parseInt(item.checked)){
							if(item.key == 'tag' && parseInt(item.showtype)){
								for(var g in item.group){
									var gitem = item.group[g];
									if(parseInt(gitem.checked)){
										var str = {
											text:gitem.catname,
											type:'tag',
											appid:gitem.appid,
											cid:gitem.cid,
											grouptag:true,
											badge:'',
											value: self.ParamScreen.value.tag.refreshval[gitem.cid] || [],
											page:1,
											keyword:'',
											loading:true,
											scrollloading:false,
											searchloading:false,
											data:[],
											finish:false
										};
										grouparr[gitem.cid] = {
											loading:true,
											data:[]
										};
										arr.push(str);
									}
								}
							}else{
								var str = {
									text:item.text,
									type:item.key,
								};
								if(self.ParamScreen.dataBadge.indexOf(item.key)>-1){
									str['badge'] = '';
								}else{
									str['dot'] = false;
								}
								arr.push(str);
							}
							index++;
						}
					}
					self.ParamScreen.groupdata = grouparr;
					self.ParamScreen.data = arr;
					var folderdata = data.folderdata;
					for(var f in folderdata){
						var fitem = folderdata[f];
						self.ScreenSubstring(fitem.pathkey,fitem.appid);
					}
					if(arr.length){
						self.ParamFooter.data.screen.show = true;
						self.CommonSetBadge();
					}
				}else{
					
				}
			},'json');
		},
		CommonCopyTxt(text){
			var self = this;
			var input = document.createElement('input'); input.setAttribute('id', 'copyInput');
			input.setAttribute('value', text);
			document.getElementsByTagName('body')[0].appendChild(input);
			document.getElementById('copyInput').select();
			document.execCommand('copy');
			document.getElementById('copyInput').remove();
			self.$toast.success('复制成功');
		}
	}
</script>